package com.kim.androidbase.db;

import java.beans.Customizer;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class IDBUtils {

	public interface QueryCompleteCallback {
		void onQueryComplete(Cursor cursor);

		void onQueryError(String errorMsg);
	}

	/**
	 * 插入单条记录
	 * 
	 * @param tableName
	 *            表名称
	 * @param contentValues
	 *            记录
	 * @param database
	 *            数据库
	 * @param conflictAlgorithm
	 *            冲突解决方法
	 */
	public static final void insert(String tableName,
			ContentValues contentValues, SQLiteDatabase database,
			int conflictAlgorithm) {
		database.insertWithOnConflict(tableName, null, contentValues,
				conflictAlgorithm);
	}

	/**
	 * 批量插入记录
	 * 
	 * @param tableName
	 *            表名称
	 * @param contentValues
	 *            记录列表
	 * @param database
	 *            数据库
	 * @param conflictAlgorithm
	 *            冲突解决方法
	 */
	public static final void insertBatch(String tableName,
			List<ContentValues> contentValuesList, SQLiteDatabase database,
			int conflictAlgorithm) {
		try {
			database.beginTransaction();
			for (ContentValues contentValues : contentValuesList) {
				database.insertWithOnConflict(tableName, null, contentValues,
						conflictAlgorithm);
			}
			database.setTransactionSuccessful();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			database.endTransaction();
		}
	}

	/**
	 * 删除记录
	 * 
	 * @param tableName
	 *            表名称
	 * @param database
	 *            数据库
	 * @param whereClause
	 *            条件
	 * @param whereArgs
	 *            条件参数
	 */
	public static void delete(String tableName, SQLiteDatabase database,
			String whereClause, String[] whereArgs) {
		try {
			database.delete(tableName, whereClause, whereArgs);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 修改记录
	 * 
	 * @param tableName
	 *            表名称
	 * @param database
	 *            数据库
	 * @param whereClause
	 *            条件
	 * @param whereArgs
	 *            条件参数
	 */
	public static void modify(String tableName, ContentValues contentValues,
			SQLiteDatabase database, String whereClause, String[] whereArgs) {
		try {
			database.update(tableName, contentValues, whereClause, whereArgs);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 查询记录
	 * 
	 * @param tableName
	 *            表名称
	 * @param database
	 *            数据库
	 * @param columns
	 *            要查询的列
	 * @param selection
	 *            查询条件
	 * @param selectionArgs
	 *            条件参数
	 * @param groupBy
	 *            分组
	 * @param having
	 *            having子句
	 * @param orderBy
	 *            排序
	 * @param queryCompleteCallback
	 *            查询回调接口，处理游标
	 */
	public static void query(String tableName, SQLiteDatabase database,
			String[] columns, String selection, String[] selectionArgs,
			String groupBy, String having, String orderBy,
			QueryCompleteCallback queryCompleteCallback) {
		Cursor cursor = null;
		try {
			cursor = database.query(tableName, columns, selection,
					selectionArgs, groupBy, having, orderBy);
			queryCompleteCallback.onQueryComplete(cursor);
		} catch (Exception e) {
			queryCompleteCallback.onQueryError(e.getMessage());
			e.printStackTrace();
		} finally {
			if (cursor != null) {
				cursor.close();
			}
		}
	}

	/**
	 * 
	 * @param sql
	 *            查询语句
	 * @param selectionArgs
	 *            条件参数
	 * @param database
	 *            数据库
	 * @param queryCompleteCallback
	 *            查询回调接口，处理游标
	 */
	public static void query(String sql, String[] selectionArgs,
			SQLiteDatabase database, QueryCompleteCallback queryCompleteCallback) {
		Cursor cursor = null;
		try {
			cursor = database.rawQuery(sql, selectionArgs);
			queryCompleteCallback.onQueryComplete(cursor);
		} catch (Exception e) {
			queryCompleteCallback.onQueryError(e.getMessage());
			e.printStackTrace();
		} finally {
			if (cursor != null) {
				cursor.close();
			}
		}
	}

	/**
	 * rawQuery
	 * 
	 * @param sql
	 * @param selectionArgs
	 * @param database
	 * @return
	 */
	public static Cursor rawQuery(String sql, String[] selectionArgs,
			SQLiteDatabase database) {
		return database.rawQuery(sql, selectionArgs);
	}

}
